package org.dbstar.scheduler.control.db.catalog;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.dbstar.scheduler.beans.Catalog;
import org.dbstar.scheduler.control.db.ObjectRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.object.SqlQuery;


/**
 * CataLog的Query对象
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-21
 */
public class CataLogQuery extends SqlQuery<Catalog> {
	private static final CatalogRowMapper catalogRowMapper = new CatalogRowMapper();

	public CataLogQuery(JdbcTemplate jdbcTemplate) {
		this(jdbcTemplate, null);
	}

	public CataLogQuery(JdbcTemplate jdbcTemplate, String condition) {
		super();
		setJdbcTemplate(jdbcTemplate);
		setSql("SELECT * FROM SCHD_CATALOG" + (condition == null ? "" : (" " + condition)));
	}

	@Override
	protected final RowMapper<Catalog> newRowMapper(Object[] parameters, @SuppressWarnings("rawtypes") Map context) {
		return catalogRowMapper;
	}

	private static class CatalogRowMapper extends ObjectRowMapper<Catalog> {
		@Override
		public Catalog mapRow(ResultSet rs, int rowNum) throws SQLException {
			Catalog info = new Catalog();
			info.setId(rs.getInt("ID"));
			info.setName(rs.getString("NAME"));
			info.setParent(getIntObject(rs, "PARENT"));
			info.setDescription(rs.getString("DESCRIPTION"));
			info.setCreateTime(rs.getLong("CREATE_TIME"));
			info.setModifyTime(getLongObject(rs, "MODIFY_TIME"));
			info.setOwner(rs.getString("OWNER"));
			info.setModifier(rs.getString("MODIFIER"));
			return info;
		}
	}
}
